﻿using System;
using System.Collections.Generic;
using System.Web;
using Artech.PetShop.Infrastructures.Interface;

namespace Artech.PetShop.Infrastructures
{
    public class NavigatorService : INavigatorService
    {
        #region INavigatorService Members

        public void Navigate(string targetUrl, IDictionary<string, object> parameters)
        {
            if (string.IsNullOrEmpty(targetUrl))
            {
                throw new ArgumentNullException("targetUrl");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("prameters");
            }

            if (parameters.Count == 0)
            {
                this.Navigate(targetUrl);
                return;
            }

            string queryString = string.Empty;
            foreach (var parameter in parameters)
            { 
                queryString += string.Format("{0}={1}&",parameter.Key, HttpUtility.UrlEncode(parameter.Value.ToString()));
            }

            queryString = queryString.TrimEnd("&".ToCharArray());
            HttpContext.Current.Response.Redirect(targetUrl + "?" + queryString);            
        }

        public void Navigate(string targetUrl)
        {
            if (string.IsNullOrEmpty(targetUrl))
            {
                throw new ArgumentNullException("targetUrl");
            }
            HttpContext.Current.Response.Redirect(targetUrl);
        }

        #endregion
    }
}